use std::io::*;
use std::fs::*;

fn main() {
    let filepath = "bigfile.log";
    let open_res = File::open(filepath);
    if open_res.is_err() {
        println!("{:?}", open_res);
        return;
    }

    let create_res = File::create(filepath.to_owned() + ".bak");
    if create_res.is_err() {
        println!("{:?}", open_res);
        return;
    }
    let newfile = create_res.unwrap();
    let mut writer = BufWriter::new(newfile);

    let file = open_res.unwrap();
    let mut reader = BufReader::new(file);
    
    loop {
        let mut line = String::new();
        let read_res = reader.read_line(&mut line);
        // println!("line{} = {}, read_res={:?}", _i, line, read_res);
        if read_res.is_ok() && read_res.unwrap() != 0 {
            if line.trim().len() > 0 {
                let write_res = writer.write((line.clone()).as_bytes());
                if write_res.is_err() {
                    break;
                }
            }
        } else {
            println!("EOF");
            break;
        }
    }
    
}